package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.article.AuthorControllerApi;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {
    @Autowired
    private AuthorService authorService;
    @PostMapping("/save")
    @Override //保存作者
    public ResponseResult save(@RequestBody ApAuthor author) {
        author.setCreatedTime(new Date());
        if(authorService.save(author)){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    @GetMapping("/findByUserId/{id}")
    @Override //根据id查询作者
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {
        List<ApAuthor> list = authorService.list(new LambdaQueryWrapper<ApAuthor>().eq(ApAuthor::getUserId, id));
        if(list!=null&&!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
    @GetMapping("/findByName/{name}")
    @Override
    public ApAuthor findByName(@PathVariable("name") String name) {
        ApAuthor apAuthor = authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        return apAuthor;
    }
}
